package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.TdmFileMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.common.utils.FileTypeUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.TdmProblemMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.ProblemResultMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.Problem;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ProblemResult;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.ProblemResultVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.ProblemResultService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * Created by sugongche on 2017/5/5.
 */
public class ProblemResultServiceImpl implements ProblemResultService {
    @Autowired
    private ProblemResultMapper problemResultMapper;
    @Autowired
    private TdmProblemMapper tdmProblemMapper;
    @Autowired
    private TdmFileMapper tdmFileMapper;

    /**
     * 保存处置结果，同时更新问题的状态
     * @param employeeVo
     * @return
     */
    @Override
    public int save(EmployeeVo employeeVo, ProblemResult problemResult, String uploadFiles, String deletedFiles) {
        int a = 0;

        Problem problem = new Problem();
        problem.setProblemId(problemResult.getProblemId());
        problem.setStatus((short) 5);
        a += tdmProblemMapper.updateByPrimaryKeySelective(problem);

        //设置主键
        problemResult.setProblemResultId(UniqueUtil.uuid());
        //必填字段
        problemResult.setCreateUserId(employeeVo.getEmployeeId());
        problemResult.setCreateTime(new Date());
        problemResult.setModifyUserId(employeeVo.getEmployeeId());
        problemResult.setModifyTime(new Date());

        a += problemResultMapper.insert(problemResult);

        if(StringUtils.isNotBlank(uploadFiles)){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);

            //重新设置附件属性
            for(int i=0; i<listFiles.size(); i++){
                File _file = listFiles.get(i);
                //设置主键
                _file.setFileId(UniqueUtil.uuid());
                //设置业务数据id
                _file.setBusinessId(problemResult.getProblemResultId());
                _file.setFileType((short) FileTypeUtil.getFileTypeByFileName(_file.getFileName()));
            }

            //必须判断非空
            if(listFiles!=null && listFiles.size()>0){
                a += tdmFileMapper.insertByList(listFiles);
            }
        }

        //需要删除的附件信息
        if(StringUtils.isNotBlank(deletedFiles)){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(deletedFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    a += tdmFileMapper.deleteByPrimaryKey(_file.getFileId());
                }
            }
        }
        return a;
    }

    /**
     * 根据问题ID查询封装问题结果类
     * @param problemId
     * @return
     */
    @Override
    public ProblemResultVo queryVoByProblemId(String problemId) {
        List<ProblemResultVo> list = problemResultMapper.selectByProblemId(problemId);

        if(list != null && list.size()>0){
            return  list.get(0);
        }else{
            return null;
        }
    }
}
